package com.whut.monitor.controller;

import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.model.MachiningEntity;
import com.whut.monitor.service.IMachiningService;
import com.whut.monitor.vo.ComboVo;
import com.whut.monitor.vo.MachiningVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CFlower on 2017/11/7.
 */
@Controller
@RequestMapping("/machining")
public class MachiningController {

    @Autowired
    private IMachiningService machiningService;

    @RequestMapping("/manager")
    public String manager() {
        return "/mintor/basicInformation/machining";
    }

    @RequestMapping("/dataGrid")
    @ResponseBody
    public Grid dataGrid(MachiningVo machine, PageFilter pageFilter) {
        Grid grid = new Grid();
        grid.setRows(machiningService.dataGrid(machine, pageFilter));
        grid.setTotal(machiningService.count(machine, pageFilter));
        return grid;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseMsg add(MachiningVo machineVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            machiningService.add(machineVo);
            j.setSuccess(true);
            j.setMsg("添加成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseMsg edit(MachiningVo machineVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            machiningService.edit(machineVo);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMsg delete(Integer id) {
        ResponseMsg j = new ResponseMsg();
        try {
            machiningService.delete(id);
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }
    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree() {
        return machiningService.tree();
    }

    @RequestMapping("/getAllMachining")
    @ResponseBody
    public List<ComboVo> getAllEquip() {
        List<ComboVo> list=new ArrayList<ComboVo>();
        List<MachiningEntity> entity=machiningService.getAllMachining();
        for (MachiningEntity machiningEntity : entity) {
            ComboVo comboVo=new ComboVo();
            comboVo.setValue(machiningEntity.getId());
            comboVo.setText(machiningEntity.getMachiningName());
            list.add(comboVo);
        }
        return list;
    }
}
